package com.zhima.controller.plain;

import com.zhima.annotation.MustLogin;
import com.zhima.dto.ContestBasicInfoDTO;
import com.zhima.dto.ZhimaOjPageResult;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.OjContestService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.zhima.dto.ZhimaOjResult.success;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhima
 * @since 2024-01-18
 */
@RestController
@RequestMapping("/contest")
@MustLogin
public class OjContestController {
    @Autowired
    private OjContestService ojContestService;

    /**
     * 分页查询比赛
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryContest(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojContestService.pageQueryContest(pageNum, pageSize));
    }

    /**
     * 分页查询我創建的比赛
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/mine/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryMineContest(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojContestService.pageQueryMineContest(UserHolder.get().getId(), pageNum, pageSize));
    }

    /**
     * 分页查询我參加的比赛
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/join/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryJoinedContest(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojContestService.pageQueryJoinedContest(UserHolder.get().getId(), pageNum, pageSize));
    }

    /**
     * 创建比赛
     *
     * @param contestBasicInfoDTO
     * @return
     */
    @PostMapping
    public ZhimaOjResult createNewContest(@RequestBody ContestBasicInfoDTO contestBasicInfoDTO) {
        return success(ojContestService.createNewContest(UserHolder.get().getId(), contestBasicInfoDTO));
    }

    /**
     * 根据{contestId}删除比赛
     *
     * @param contestId
     * @return
     */
    @DeleteMapping("/{contestId}")
    public ZhimaOjResult deleteContestById(@PathVariable("contestId") Long contestId) {
        return success(ojContestService.deleteContestById(contestId));
    }

    /**
     * 根据{contestId}获取比赛简略信息
     *
     * @param contestId
     * @return
     */
    @GetMapping("/{contestId}")
    public ZhimaOjResult getContestById(@PathVariable("contestId") Long contestId) {
        return success(ojContestService.getContestById(UserHolder.get(), contestId));
    }

    @GetMapping("/edit/{contestId}")
    public ZhimaOjResult queryContestEditById(@PathVariable("contestId") Long contestId) {
        return success(ojContestService.queryContestEditById(UserHolder.get().getId(), contestId));
    }

    /**
     * 登录用户报名参加比赛
     *
     * @param contestId
     * @return
     */
    @PostMapping("/apply/{contestId}/{inviteCode}")
    public ZhimaOjResult applyContest(@PathVariable("contestId") Long contestId, @PathVariable("inviteCode") String inviteCode) {
        return success(ojContestService.applyContest(contestId, inviteCode));
    }

    /**
     * 更新比赛基本信息
     *
     * @param contestBasicInfoDTO
     * @return
     */
    @PutMapping
    public ZhimaOjResult updateContestBasicInfo(@RequestBody ContestBasicInfoDTO contestBasicInfoDTO) {
        return success(ojContestService.updateContestBasicInfo(UserHolder.get().getId(), contestBasicInfoDTO));
    }
}
